package com.panlf.Thread基础;


import java.util.concurrent.TimeUnit;

/**
 * join 的用法
 * @author panlf
 * @since 2024/1/10 10:38
 */
public class JoinTest {

    public static void main(String[] args) throws InterruptedException {
//        log.debug("start...");
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2");

        /**
         * join 的本质调用的是 wait(0) 方法, 但是会先判断线程是否存活
         *  while (isAlive()) {
         *        wait(0);
         *  }
         *
         *  join 方法用 synchronized 修饰, 锁对象就是 t1
         *
         */
        t2.join();
        t1.join();


    }
}
